package com.culture.bootdemo.controller.thread;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author: wen
 * @date 2020/10/27
 * 多线程 @Async使用：
 */
@RestController
@RequestMapping("/threadAsync")
@Slf4j
//@EnableAsync  该注解设置在启动类
public class ThreadAsyncController {

    @Autowired
    private DemoService demoService;

    @GetMapping("testTestAsy")
    public void testTestAsy() {
        String mainThreadName = Thread.currentThread().getName();
        long mainThreadId = Thread.currentThread().getId();
        System.out.println("------We got main thread: " + mainThreadName + " - " + mainThreadId + "----------");
        demoService.testAsy();
    }


    @Component
    public class DemoService {
        //公共配置在com.culture.bootdemo.controller.thread.ThreadPoolConfig类中
        @Async("taskExecutor")
        public void testAsy() {
            String asyThreadName = Thread.currentThread().getName();
            long asyThreadId = Thread.currentThread().getId();
            System.out.println("======Async====");
            System.out.println("------We got asy thread: " + asyThreadName + " - " + asyThreadId + "----------");
        }
    }
}
